package com.item.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import com.base.CoreConstants;
import com.base.support.PropertySupport;
import com.base.util.JSONUtils;

public class JpushUtil {
	private static final Logger logger = Logger.getLogger(JpushUtil.class);

	private static Map<String, JPushClient> clients = new HashMap<String,JPushClient>();
	public static Map<String,Integer> deployStatuses = new HashMap<String, Integer>();

	public static JPushClient getClient(String appid){
		JPushClient client = clients.get(appid);
		if(client == null){
			String appkey = PropertySupport.getProperty(appid+".appkey");
			String master = PropertySupport.getProperty(appid+".master");
			client = new JPushClient(master,appkey);
			clients.put(appid, client);
			deployStatuses.put(appid, NumberUtils.toInt(CoreConstants.getProperty(appid+".deployStatus")));
		}
		return client;
	}

	//根据别名推送安卓单播消息
	public static void sendAndroidMessageWithAlias(String appid,String title, Map<String , String> extras, String alias){
		try {
			JPushClient client = getClient(appid);
			alias = alias.replaceAll("-", "_");
			String content = JSONUtils.serialize(extras);
			PushResult  msgResult = client.sendAndroidMessageWithAlias(title, content, alias);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.info(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}

	//根据别名推送安卓单播通知
	public static void sendAndroidNotificationWithAlias(String appid,String title, String content, Map<String , String> extras, String alias){
		try {
			JPushClient client = getClient(appid);
			alias = alias.replaceAll("-", "_");
			PushResult  msgResult = client.sendAndroidNotificationWithAlias(title, content, extras, alias);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}

	//根据tag推送安卓组播通知
	public static void sendAndroidNotificationWidhTag(String appid, String tag, String title, String content, Map<String,String> params){
		try {
			JPushClient client = getClient(appid);
			tag = tag.replaceAll("-", "_");
			PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag(tag)).setNotification(Notification.android(content, title, params)).build();
			PushResult  msgResult  = client.sendPush(pl);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}

	//根据tag推送安卓组播消息
	public static void sendAndroidMessageWidhTag(String appid, String tag, Map<String,String> params){
		try {
			JPushClient client = getClient(appid);
			tag = tag.replaceAll("-", "_");
			String content = JSONUtils.serialize(params);
			PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag(tag)).setMessage(Message.content(content)).build();
			PushResult  msgResult  = client.sendPush(pl);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}

	//根据alias推送IOS单播通知
	public static void sendIosNotificationWithAlias(String appid,String content, Integer badge, Map<String , String> params, String alias){
		try {
			JPushClient client = getClient(appid);
			alias = alias.replaceAll("-", "_");
			Options options = null;
			if(deployStatuses.get(appid) == 1){
				options = Options.newBuilder().setApnsProduction(true).build();
			}else{
				options = Options.newBuilder().setApnsProduction(false).build();
			}
			PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.alias(alias)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().addExtras(params).setAlert(content).setBadge(badge).build()).build()).setOptions(options).build();
			PushResult  msgResult  = client.sendPush(pl);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}
	
	//根据别名推送安卓单播消息
		public static void sendAndroidMessage(String appid, Map<String , String> extras){
			try {
				JPushClient client = getClient(appid);
				String content = JSONUtils.serialize(extras);
				PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all()).setMessage(Message.content(content)).build();
				PushResult  msgResult = client.sendPush(pl);
				if(msgResult != null){
					if(msgResult.isResultOK()){
						logger.debug("推送成功");
					}else{
						logger.debug("推送失败");
					}
				}else{
					logger.debug("无法获取数据");
				}
			} catch (APIConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (APIRequestException e) {
				// TODO Auto-generated catch block
				logger.info(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
			}
		}

		//根据别名推送安卓单播通知
		public static void sendAndroidNotification(String appid,String title,  Integer badge, String content, Map<String , String> extras){
			try {
				JPushClient client = getClient(appid);
				PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all()).setNotification(Notification.android(content, title, extras)).build();
				PushResult  msgResult = client.sendPush(pl);
				if(msgResult != null){
					if(msgResult.isResultOK()){
						logger.debug("推送成功");
					}else{
						logger.debug("推送失败");
					}
				}else{
					logger.debug("无法获取数据");
				}
			} catch (APIConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (APIRequestException e) {
				// TODO Auto-generated catch block
				logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
			}
	   }

	//根据alias推送IOS单播消息
	public static void sendIosMessageWithAlias(String appid,String title, Map<String , String> extras, String alias){
		try {
			JPushClient client = getClient(appid);
			alias = alias.replaceAll("-", "_");
			String content = JSONUtils.serialize(extras);
			Options options = null;
			if(deployStatuses.get(appid) == 1){
				options = Options.newBuilder().setApnsProduction(true).build();
			}else{
				options = Options.newBuilder().setApnsProduction(false).build();
			}
			PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.alias(alias)).setMessage(Message.content(content)).setOptions(options).build();
			PushResult  msgResult  = client.sendPush(pl);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}

	//根据tag推送IOS组播通知
	public static void sendIosNotificationWidhTag(String appid, String tag, Integer badge, String content, Map<String,String> params){
		try {
			JPushClient client = getClient(appid);
			tag = tag.replaceAll("-", "_");
			Options options = null;
			if(deployStatuses.get(appid) == 1){
				options = Options.newBuilder().setApnsProduction(true).build();
			}else{
				options = Options.newBuilder().setApnsProduction(false).build();
			}
			PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag(tag)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().addExtras(params).setAlert(content).setBadge(badge).build()).build()).setOptions(options).build();
			PushResult  msgResult  = client.sendPush(pl);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}

	//根据tag推送IOS组播消息
	public static void sendIosMessageWithTag(String appid, String tag, Map<String,String> params){
		try {
			JPushClient client = getClient(appid);
			tag = tag.replaceAll("-", "_");
			Options options = null;
			if(deployStatuses.get(appid) == 1){
				options = Options.newBuilder().setApnsProduction(true).build();
			}else{
				options = Options.newBuilder().setApnsProduction(false).build();
			}
			String content = JSONUtils.serialize(params);
			PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag(tag)).setMessage(Message.content(content)).setOptions(options).build();
			PushResult  msgResult  = client.sendPush(pl);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}
	
	//推送IOS广播通知
	public static void sendIosNotification(String appid, String content,  Integer badge, Map<String,String> params){
		try {
			JPushClient client = getClient(appid);
			Options options = null;
			if(deployStatuses.get(appid) == 1){
				options = Options.newBuilder().setApnsProduction(true).build();
			}else{
				options = Options.newBuilder().setApnsProduction(false).build();
			}
			PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all()).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().addExtras(params).setAlert(content).setBadge(badge).build()).build()).setOptions(options).build();
			Notification.ios_set_badge(badge);
			PushResult  msgResult  = client.sendPush(pl);
			if(msgResult != null){
				if(msgResult.isResultOK()){
					logger.debug("推送成功");
				}else{
					logger.debug("推送失败");
				}
			}else{
				logger.debug("无法获取数据");
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
		}
	}
	
	//推送IOS广播消息
	public static void sendIosMessage(String appid, Map<String,String> params){
			try {
				JPushClient client = getClient(appid);
				Options options = null;
				if(deployStatuses.get(appid) == 1){
					options = Options.newBuilder().setApnsProduction(true).build();
				}else{
					options = Options.newBuilder().setApnsProduction(false).build();
				}
				String content = JSONUtils.serialize(params);
				PushPayload pl = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all()).setMessage(Message.content(content)).setOptions(options).build();
				PushResult  msgResult  = client.sendPush(pl);
				if(msgResult != null){
					if(msgResult.isResultOK()){
						logger.debug("推送成功");
					}else{
						logger.debug("推送失败");
					}
				}else{
					logger.debug("无法获取数据");
				}
			} catch (APIConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (APIRequestException e) {
				// TODO Auto-generated catch block
				logger.debug(String.format("request_id: %d, error_code: %d, error_message: %s",e.getMsgId(), e.getErrorCode(),e.getErrorMessage()));
			}
	}

}
